<template>
  <div>
    <el-dropdown trigger="click" placement="bottom" ref="messageDrop">
      <div
        class="message-box-container"
        :style="{ color: defaultColor ? defaultColor : systemInfo.colorValue }"
      >
        <i class="el-icon-bell" />
        <div
          class="all-count"
          v-if="totalCount.noticeTotal + totalCount.alarmTotal > 0"
        >
          {{
            totalCount.noticeTotal + totalCount.alarmTotal > 99
              ? "99+"
              : totalCount.noticeTotal + totalCount.alarmTotal
          }}
        </div>
      </div>
      <el-dropdown-menu slot="dropdown">
        <div class="msg-box">
          <div class="msg-text" @click="goMessage(1)">
            公告
            <div class="count" v-if="totalCount.noticeTotal > 0">
              {{ totalCount.noticeTotal > 99 ? "99+" : totalCount.noticeTotal }}
            </div>
          </div>
          <!-- <div class="msg-text" @click="goMessage(2)">
            通知
            <div class="count" v-if="totalCount.tongTotal > 0">
              {{ totalCount.tongTotal > 99 ? "99+" : totalCount.tongTotal }}
            </div>
          </div> -->
          <div class="msg-text" @click="goMessage(3)">
            告警
            <div class="count" v-if="totalCount.alarmTotal > 0">
              {{ totalCount.alarmTotal > 99 ? "99+" : totalCount.alarmTotal }}
            </div>
          </div>
          <div class="msg-button">
            <el-button type="text" @click="goMessage('all')"
              >查看全部</el-button
            >
          </div>
        </div>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import { fetchNoticeCount } from "@/api/system/publicNotice";

export default {
  name: "index",
  props: {
    defaultColor: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      list: [],
      info: {
        sysMsgTitle: "",
        sysMsgContent: "",
        createTime: "",
      },
    };
  },
  computed: {
    ...mapGetters(["systemInfo"]),
    ...mapState("notice", ["totalCount"]),
  },
  methods: {
    getNoticeCount() {
      fetchNoticeCount().then((res) => {
        if (res && res.code === 200) {
          this.$store.commit("notice/SET_TOTAL_COUNT", res.data || {});
        }
      });
    },

    goMessage(flag) {
      this.$bus.$emit("NOTICE_CLICK");
      this.$router.push({
        path: "/msg/index?currentTab=" + flag,
      });
      this.$refs.messageDrop.hide();
    },
  },
  beforeDestroy() {
    //关闭 获取通知个数
    // this.$bus.$off("messageList");
  },
  mounted() {
    this.getNoticeCount();

    // this.$bus.$on("messageList", () => {
    //   //获取通知个数
    //   this.getNoticeCount();
    // });
  },
};
</script>

<style scoped lang="scss">
.message-box-container {
  font-size: 18px;
  height: 100%;
  color: #fff;
  cursor: pointer;
  position: relative;
  .all-count {
    width: 18px;
    height: 18px;
    background: #f56c6c;
    border-radius: 50%;
    color: #fff;
    line-height: 18px;
    text-align: center;
    position: absolute;
    top: -2px;
    right: -7px;
    font-size: 10px;
  }
}

.msg-box {
  width: 120px;

  .msg-text {
    width: 100%;
    height: 40px;
    padding: 12px;
    display: flex;
    font-size: 12px;
    align-items: center;
    color: #606266;
    cursor: pointer;
    border-bottom: 1px solid #eee;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .count {
      width: 18px;
      height: 18px;
      background: #f56c6c;
      border-radius: 50%;
      color: #fff;
      line-height: 18px;
      text-align: center;
      font-size: 10px;
    }
  }

  .msg-box-title {
    padding: 12px;
    box-sizing: border-box;
    border-bottom: 1px solid #eee;
    font-weight: 400;
    font-size: 14px;
  }

  .msg-button {
    line-height: 20px;
    text-align: center;
    cursor: pointer;

    width: 100%;
    height: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 12px 0 6px 0;
  }
}
</style>
